package com.lsq.asyndemo.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

@Service
public class OrderService {

    public String createOrder(String orderId) {
        try {
            Thread.sleep(3000L);
            System.out.println("创建订单" + orderId);
//            int k = 1 / 0;
            return "执行成功";
        } catch (Exception e) {
            System.out.println("创建订单失败");
            throw new RuntimeException(e);
        }
    }

    @Async("taskExecutor")
    public CompletableFuture<String> createOrder1(String orderId) {
        try {
            Thread.sleep(3000L);
            System.out.println("创建订单" + orderId);
//            int k = 1 / 0;
            return CompletableFuture.completedFuture("创建订单" + orderId);
        } catch (Exception e) {
            System.out.println("创建订单失败");
            throw new RuntimeException(e);
        }
    }

    public double calculaePrice(double price) {
        try {
            Thread.sleep(2000L);
            double price1 = price + 2;
            int k = 1/0;
            System.out.println("计算价格:" + price1);
            return price1;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Async("taskExecutor")
    public CompletableFuture<String> calculaePrice1(double price) {
        try {
            Thread.sleep(2000L);
            double price1 = price + 2;
            System.out.println("计算价格:" + price1);
            return CompletableFuture.completedFuture("计算价格:" + price1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public String sendMsg(double price) {
        try {
            Thread.sleep(3000L);
            System.out.println("发送邮件" + price);
            return "执行成功";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
